package game 
{
	import assets.Assets;
	import flash.geom.Rectangle;
	import game.core.GameManager;
	import game.core.Player;
	import game.core.races.DwarfRace;
	import game.core.races.MageRace;
	import game.ground.Ground;
	import game.gui.Camera;
	import game.gui.Gui;
	import game.objects.farms.Farm;
	import game.objects.farms.Pylon;
	import game.objects.Tester;
	import game.objects.towers.Tower;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Tilemap;
	import net.flashpunk.World;
	/**
	 * ...
	 * @author rnk
	 */
	public class GameWorld extends World
	{
		public var gui:Gui;
		public var ground:Ground;
		public var gamemanager:GameManager;
		
		public function GameWorld() 
		{
			
		}
		
		override public function begin():void 
		{
			//var tilemap:Tilemap = new Tilemap(tileset, 32 * 10, 32 * 10, 32, 32);

			ground = new Ground(Assets.MAP1_OEL);
			add(ground);
			
			gui = new Gui();
			FP.engine.addChild(gui);
			gui.Init(this);
			
			gamemanager = new GameManager();
			gamemanager.Init(this, [new Player("Симон зи Сорсерер", MageRace), new Player("Двалин Мейджкрашер", DwarfRace)]);
			
			for (var i:int = 0; i < 2; i++)
			{
				var tower:Tower = new Tower();
				tower.player = gamemanager.players[0];
				tower.x = ground.startingPoses[i].x*32;
				tower.y = ground.startingPoses[i].y*32;
				gamemanager.AddBuilding(tower);
			}
			var pylon:Pylon = new Pylon();
			pylon.player = gamemanager.players[0];
			pylon.x = ground.startingPoses[5].x*32;
			pylon.y = ground.startingPoses[5].y*32;
			gamemanager.AddBuilding(pylon);
			
			
			for (i = 3; i < 5; i++)
			{
				tower = new Tower();
				tower.player = gamemanager.players[1];
				tower.x = ground.startingPoses[i].x*32;
				tower.y = ground.startingPoses[i].y*32;
				gamemanager.AddBuilding(tower);
			}
			
			var farm:Farm = new Farm();
			farm.player = gamemanager.players[1];
			farm.x = ground.startingPoses[2].x*32;
			farm.y = ground.startingPoses[2].y*32;
			gamemanager.AddBuilding(farm);
			
			
			//var tester:Tester = new Tester(200, 200);
			//add(tester);
			
			//camera.SetBounds(new Rectangle(0,0,ground.width,ground.height));
			//gui.camera.FollowTarget(tester);
			
			
			gamemanager.StartGame();
		}
		
		override public function end():void 
		{
			
			gui.Die();
			FP.engine.removeChild(gui);
			gamemanager.Die();
			super.end();
		}
		
		override public function update():void 
		{
			gui.Update();
			gamemanager.Update();
			super.update();
		}
		
		
	}

}